package views;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.SpringLayout;
import javax.swing.JSeparator;
import views.accounting.AccountingView;
import views.bill.BillView;
import views.crm.CrmView;
import views.inventory.InventoryView;
import views.report.ReportView;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Dimension;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import dbhandeling.Connections;
import java.awt.Cursor;

public class MainView extends JFrame implements MouseListener 
{
	private static final long serialVersionUID = 1L;
	private SpringLayout springLayout;
    private JSeparator separatorHeader;
    private JSeparator separatorFooter;
    private JLabel[]headerMenu;
  
	public static void main(String[] args) 
	{
		Connections.loadDriver();
		Connections.connect();
		
		EventQueue.invokeLater(new Runnable() 
		{
			public void run() {
				try 
				{
					UIManager.setLookAndFeel(
					        UIManager.getSystemLookAndFeelClassName());
					MainView frame = new MainView();
					frame.setVisible(true);
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
			}
		});
	}

	public MainView() 
	{
		getContentPane().setForeground(Color.WHITE);
		getContentPane().setFont(new Font("MS Reference Sans Serif", Font.PLAIN, 13));
		getContentPane().setBackground(Color.WHITE);
		springLayout = new SpringLayout();
		getContentPane().setLayout(springLayout);
		
		// make string array of labels
		// using that array and size elements call makeHeaderMenu
		// call styling method
		// call set listner method
		
		separatorHeader = new JSeparator();
		separatorHeader.setForeground(Color.PINK);
		springLayout.putConstraint(SpringLayout.NORTH, separatorHeader, 100, SpringLayout.NORTH, getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, separatorHeader, 0, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, separatorHeader, 0, SpringLayout.EAST, getContentPane());
		getContentPane().add(separatorHeader);
		
		String[] labelNames={"HOME","INVENTORY","BILL","REPORTS","ACCOUNTING","CRM"};
		Dimension[]prefferedSize={new Dimension(70, 30),new Dimension(110, 30),new Dimension(60, 30),new Dimension(90, 30),new Dimension(120, 30),new Dimension(60, 30)};
		this.makeHeaderMenu(labelNames, 50, 2, 0, prefferedSize);
		Font font=new Font("MS Reference Sans Serif", Font.BOLD, 13);
		Border border= new LineBorder(Color.PINK, 1, true);
	    this.headerMenuStyling(Cursor.HAND_CURSOR, Color.WHITE,border, Color.PINK, font);
		this.addListeners();
		
		separatorFooter = new JSeparator();
		springLayout.putConstraint(SpringLayout.SOUTH, separatorFooter, -30, SpringLayout.SOUTH, getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, separatorFooter, 0, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, separatorFooter, 0, SpringLayout.EAST, getContentPane());
		getContentPane().add(separatorFooter);
	
	    //this.setBounds(100, 100, 1050, 650);
	    this.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	private void makeHeaderMenu(String[]labelNames,int leftdistance,int horizontalgap,int verticalGap,Dimension[]prefereedSize)
	{	
		headerMenu=new JLabel[labelNames.length];
		// initalize Array;
		for(int i=0;i<headerMenu.length;i++)
		{
			headerMenu[i]=new JLabel(labelNames[i],JLabel.CENTER);		
		}
		
		// initialize left component
		springLayout.putConstraint(SpringLayout.WEST, headerMenu[0],leftdistance , SpringLayout.WEST, getContentPane());
		
		// set left constrains
		for(int i=1;i<headerMenu.length;i++)
			springLayout.putConstraint(SpringLayout.WEST, headerMenu[i], horizontalgap, SpringLayout.EAST, headerMenu[i-1]);
		
		//for south constrain from headerSeprator
		for(int i=0;i<headerMenu.length;i++)
		{
			springLayout.putConstraint(SpringLayout.SOUTH, headerMenu[i], verticalGap, SpringLayout.NORTH, separatorHeader);
		    getContentPane().add(headerMenu[i]);
		}
		
		for(int i=0;i<headerMenu.length;i++)
		{
			headerMenu[i].setPreferredSize(prefereedSize[i]);
		}
	}
	
	
	private void headerMenuStyling(int cursorType,Color fgcolortype,Border border,Color bgcolortype,Font font)
	{
		// change Cursor and fonts color
		for(int i=0;i<headerMenu.length;i++)
		{
			headerMenu[i].setCursor(Cursor.getPredefinedCursor(cursorType));
			headerMenu[i].setForeground(fgcolortype);
			headerMenu[i].setBorder(border);
			headerMenu[i].setOpaque(true);
			headerMenu[i].setBackground(bgcolortype);
			headerMenu[i].setFont(font);
		}
	}
	
	// Add Listeners
	private void addListeners()
	{
		for(int i=0;i<headerMenu.length;i++)
		{
			headerMenu[i].addMouseListener(this);
		}
			
	}
	
	private void stickPanel(JPanel panel,int headerGap,int footerGap,int componentFixedCount)
	{	
		if(getContentPane().getComponentCount()==componentFixedCount)
			 getContentPane().remove(componentFixedCount-1);
		
		springLayout.putConstraint(SpringLayout.WEST, panel, 0, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, panel, 0, SpringLayout.EAST, getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, panel, -footerGap, SpringLayout.SOUTH,separatorFooter);
		springLayout.putConstraint(SpringLayout.NORTH, panel,headerGap , SpringLayout.NORTH,separatorHeader);
		getContentPane().add(panel);
		getContentPane().validate();
		getContentPane().repaint();
	}


	@Override
	public void mouseClicked(MouseEvent arg0) 
	{
		JLabel button=(JLabel) arg0.getSource();
		String labelTitle=button.getText();
		
	    if(labelTitle.equals("HOME"))
	    {
	    	stickPanel(new JPanel(),2,2,9);	    	
	    }
	    if(labelTitle.equals("INVENTORY"))
	    {
	    	stickPanel(new InventoryView(),2,2,9);
	    }
	    if(labelTitle.equals("BILL"))
	    {
	    	stickPanel(new BillView(),2,2,9);
	    }
	    if(labelTitle.equals("REPORTS"))
	    {
	    	stickPanel(new ReportView(),2,2,9);
	    }
	    if(labelTitle.equals("ACCOUNTING"))
	    {
	    	stickPanel(new AccountingView(),2,2,9);
	    }
	    if(labelTitle.equals("CRM"))
	    {
	    	stickPanel(new CrmView(),2,2,9);
	    }
	}


	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
	
		